package com.yuanss.smart.framework.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description Servlet助手类
 * @Date 2024/11/19 下午4:42
 * @Created by 菜到不敢run
 */
public final class ServletHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServletHelper.class);

    private static final ThreadLocal<ServletHelper> HELPER_HOLDER = new ThreadLocal<ServletHelper>();

    private HttpServletRequest request;

    private HttpServletResponse response;

    public ServletHelper() {
    }

    public ServletHelper(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    /**
     * 初始化
     * @param request
     * @param response
     */
    public static void init(HttpServletRequest request, HttpServletResponse response) {
        HELPER_HOLDER.set(new ServletHelper(request, response));
    }

    public static void remove() {
        HELPER_HOLDER.remove();
    }

    /**
     * 获取request
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return HELPER_HOLDER.get().request;
    }

    /**
     * 获取response
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        return HELPER_HOLDER.get().response;
    }

    /**
     * 获取session
     * @return HttpSession
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取ServletContext
     * @return ServletContext
     */
    public static ServletContext getServletContext() {
        return getRequest().getServletContext();
    }

    /**
     * 将属性放入request域
     * @param key
     * @param value
     */
    public static void setRequestAttribute(String key, Object value) {
    	getRequest().setAttribute(key, value);
    }

    /**
     * 从request域获取属性
     * @param key
     * @return
     */
    public static Object getRequestAttribute(String key) {
    	return getRequest().getAttribute(key);
    }

    /**
     * 移除request域的属性
     * @param key
     */
    public static void removeRequestAttribute(String key) {
    	getRequest().removeAttribute(key);
    }

    /**
     * 重定向
     * @param contextPath
     */
    public static void sendRedirect(String contextPath) {
        try {
            getResponse().sendRedirect(getRequest().getContextPath() + contextPath);
        } catch (Exception e) {
            LOGGER.error("redirect error", e);
        }
    }

    /**
     * 将属性放入session域
     * @param key
     * @param value
     */
    public static void setSessionAttr(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    /**
     * 从session域获取属性
     * @param key
     * @return
     * @param <T>
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSessionAttr(String key) {
        return (T) getSession().getAttribute(key);
    }

    /**
     * 移除session域的属性
     * @param key
     */
    public static void removeSessionAttr(String key) {
        getSession().removeAttribute(key);
    }

    /**
     * 销毁session
     */
    public static void invalidateSession() {
        getSession().invalidate();
    }
}
